package com.yangrd.airship.sys.web.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

/**
 * description: SwaggerConfig
 *
 * @author: yangrd
 * @date: 2021/4/14
 * @version: 1.0
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Bean
    public Docket webDocket(@Autowired(required = false) OpenApiExtensionResolver openApiExtensionResolver) {
        return webDocket();/*.extensions(
                Objects.isNull(openApiExtensionResolver) ?
                        Collections.emptyList() : openApiExtensionResolver.buildExtensions("web 1.0")
        );*/
    }


    public Docket webDocket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("web 1.0")
                .select()
                //RequestHandlerSelectors配置扫描接口的方式（还有any，none等），
                //basePackage：指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.yangrd.airship"))
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 配置swagger-ui.html的页面信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfo(
                "airship Api文档",
                "airship",
                "v1.0",
                "",
                new Contact("yangrd", "#", "yangrd_1992@sina.com"),
                "http://www.apache.org/licenses/LICENSE-2.0",
                "LICENSE",
                new ArrayList<>()
        );
    }
}
